<!DOCTYPE html>
<html lang="en">
<title>私教轮排列表</title>
<#import "/layout/layout.html" as body/>
<@body.page>
<style>
    .dataTables_empty {
        display: none
    }
</style>

<!--<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>教练管理</h2>
        <ol class="breadcrumb">
            <li><a href="#">教练管理</a></li>
            <li class="active"><strong>教练列表</strong></li>
        </ol>
    </div>
</div>-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">私教轮排列表</h5>
                </div>
                <div class="ibox-content is-drag">
                    <div class="table-responsive">
                        <form class="form-inline">

                            <div class="form-group">
                                <label for="id">教练 ID：</label>
                                <input class="form-control" id="id" name="id" placeholder="输入教练ID" />
                            </div>

                            <div class="form-group">
                                <label for="nickName">姓名：</label>
                                <input class="form-control" id="nickName" name="nickName" placeholder="输入教练名字" />
                            </div>


                            <button type="button" class="btn btn-outline btn-primary" style="margin-top: 5px" onClick="coachPage(1)">确认</button>
                        </form>
                        <hr>
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>教练ID</th>
                                <th>教练姓名</th>
                                <th>当前轮排订单</th>
                                <th>当前轮排圈数</th>
                                <th>当前圈排序</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="list">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：<span id="coachCount">0</span>位教练</h4>
                    <!--<button type="button" onclick="sort('coachList')" class="btn btn-success pull-right">确定修改排序</button>-->
                </div>

            </div>
        </div>
    </div>
</div>

<script>

    $(document).ready(function () {
        //导出excel
//        ExampleFile("教练excel");

        coachPage(1);

        $(document).keypress(function (e) {
            // 回车键事件
            if (e.which === 13) {
                coachPage(1);
            }
        });
        //拖动排序
        $(".is-drag").sortable({
            cursor: "move",
            items: ".is-drag-li",               //只是li可以拖动
            opacity: 0.6,                       //拖动时，透明度为0.6
            revert: false,                       //释放时，增加动画
            update: function (event, ui) {       //更新排序之后
                console.log($(this).sortable("toArray"));
            }
        });
    });

    //分页方法
    function coachPage(currentIndex) {
        var param = {
            "pageIndex": currentIndex,
            "pageSize": 10,
            "id": $("#id").val(),
            "nickName": $("#nickName").val(),
            "mobile": $("#mobile").val()
        };

        $("#coachList").empty();
        $.post(
            "${baseUrl}system/coach/coach_private_sort_list",
            param,
            function (result) {

                var html = "";
                //分页
                /*new Page({
                    parent: $("#copot-page"),
                    nowPage: currentIndex,
                    pageSize: 10,
                    totalCount: result.coachCount,
                    setting: {
                        defaultPage: 5,//默认显示可点击分页的个数
                        firstPageText: "首页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        lastPageText: "尾页"
                    },
                    callback: function (nowPage, totalPage) {
                        coachPage(nowPage);
                    }
                });*/

                $("#coachCount").text(result.count);
                var orderId;
                var updatedTime;
                $.each(result.list, function (i, data) {
                    if ( data.orderId == undefined) {
                        orderId = "";
                    } else {
                        orderId = data.orderId;
                    }
                    if (data.updatedTime == undefined) {
                        updatedTime = "";
                    } else {
                        updatedTime = data.updatedTime;
                    }

                    html += "<tr class='is-drag-li'>";
                    html += "<td id='hf_" + data.id + "_" + (i + 1) + "'>" + (i + 1) + "</td>";
                    html += "<td>" + data.coachId + "</td>";
                    html += "<td>" + data.nickName + "</td>";
                    html += "<td>" + orderId + "</td>";
                    html += "<td>" + data.sortNow + "</td>";
                    html += "<td>" + data.sortNum + "</td>";
                    html += "<td>" + data.createdTime + "</td>";
                    html += "<td>" + updatedTime + "</td>";
                    html += "<td width='270'>";
                    html += "<button type='button' onclick='removeSort(" + data.coachId + ")' class='btn btn-default'><i class='fa fa-ban''></i> 从轮排从移除 </button>";
                    html += "<button type='button' onclick='editCoach(" + data.coachId + ")' class='btn btn-default'><i class='fa fa-edit''></i> 编辑教练 </button>";
                    html += "</td>";
                    html += "</tr>";
                });
                $("#list").html(html);
            }

        );
    }

    //教练编辑
    function editCoach(coachId) {
       window.location = "${baseUrl}system/coach/to_coach_edit?id=" + coachId;
    }

    //移除轮排
    function removeSort(coachId) {

        swal({
            title: "确定要移除吗?",
            text: "移除后该教练将不再参加轮排!",
            type: "question",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            cancelButtonText: "容我三思！",
            confirmButtonText: "移除!"
        }).then(function () {
            $.post("coach_remove_sort", {coachId: coachId}, function (result) {
                if (result) {
                    swal("移除成功!", "页面即将重新加载,请稍后...", "success");
                    window.setTimeout(function () {
                        window.location.reload()
                    }, 1000);
                }
            });
        },function () {
        });
    }

    /**
     * 私教教练排序
     * @param id listId
     * @author blank 2017/8/7 下午4:27
     */
    function sort(id) {
        var sort_string = "";
        $("#"+ id +"").find("td").each(function () {
            var val = $(this).attr("id");
            sort_string = sort_string + "##" + val;
        });
        $.ajax({
            url: "sort",
            data: "sortString=" + sort_string,
            type: "POST",
            success: function (result) {
                if (result) {
                    swal({
                        title:"排序修改成功!",
                        timer:1000
                    }).then(
                        function () {},
                        // handling the promise rejection
                        function (dismiss) {
                            if (dismiss === 'timer') {
//                                console.log('I was closed by the timer')
                            }
                        }
                    );
                    coachPage(1);
                }
            }
        });
    }

</script>

</@body.page>
</html>